home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Papers / Rentzsch / Code / Align / Align.h < prev    next >
Encoding:
Text File  |  2001-06-23  |  6.7 KB  |  247 lines

  1. /****************************************************************************************
  2.     Align.h
  3.         <http://redshed.net/align>
  4.     
  5.         Straighten up and fly right
  6.             Straighten up and fly right
  7.         Straighten up and fly right
  8.             Cool down papa, don't you blow your top!
  9.     
  10.     Copyright © 1999-2001 Red Shed Software. All rights reserved.
  11.     by Jonathan 'Wolf' Rentzsch (jon@redshed.net)
  12.  
  13.         Redistribution and use in source and binary forms, with or without modification,
  14.         are permitted provided that the following conditions are met:
  15.  
  16.         1. Redistributions of source code must retain the above copyright notice, this
  17.         list of conditions and the following disclaimer.
  18.  
  19.         2. Redistributions in binary form must reproduce the above copyright notice, this
  20.         list of conditions and the following disclaimer in the documentation and/or other
  21.         materials provided with the distribution.
  22.  
  23.         3. The names and trademarks of copyright holders may not be used in advertising
  24.         or publicity pertaining to the software without specific prior permission.
  25.  
  26.         This software is provided "as is" and any expressed or implied warranties,
  27.         including, but not limited to, the implied warranties of merchantability and
  28.         fitness for a particular purpose are disclaimed.  In no event shall the authors
  29.         or copyright holders be liable for any direct, indirect, incidental, special,
  30.         exemplary, or consequential damages (including, but not limited to, procurement
  31.         of substitute goods or services; loss of use, data, or profits; or business
  32.         interruption) however caused and on any theory of liability, whether in contract,
  33.         strict liability, or tort (including negligence or otherwise) arising in any way
  34.         out of the use of this software, even if advised of the possibility of such
  35.         damage.
  36.     
  37.     Commenter    Date                Comment
  38.     ---------    -----------------    -----------------------------------------------------
  39.     wolf        Fri, Feb 12, 1999    Created.
  40.     wolf        Thu, Apr 1, 1999    Added Aligned template.
  41.     wolf        Mon, Apr 12, 1999    Added AlignX().
  42.                                     Rewrote Aligned template. Now supports 2, 8 and 16
  43.                                     byte-aligned data as well as 4.
  44.     wolf        Tue, Apr 13, 1999    Rewrote NewAlignedPtr() to take alignment as a
  45.                                     literal variable instead of a flag. This makes it
  46.                                     more symmetrical.
  47.     wolf        Fri, May 7, 1999    The Aligned template worked well for C data
  48.                                     structures but contained a flaw for C++ constructed
  49.                                     objects. The Constructor would be called on the
  50.                                     non-aligned pointer and then the pointer would be
  51.                                     aligned (moved). The result would that the
  52.                                     Constructor operated on the wrong section of memory.
  53.                                     
  54.                                     I now align the pointer *and then* call "placement
  55.                                     new" to call the Constructor on the correct section.
  56.                                     
  57.                                     The old Aligned template is still included under the
  58.                                     name OldAligned.
  59.                                     
  60.                                     Broke out Aligned and OldAligned templates into
  61.                                     their own header file, Aligned.h.
  62.     wolf        Mon, Nov 1, 1999    Added AlignDirect_(), which is functionally
  63.                                     identical to Align_() (where _ is either 2, 4, 8 or
  64.                                     16) except it takes a pointer as its parameter and
  65.                                     returns a pointer. This is unlike Align_(), which
  66.                                     takes a pointer to a pointer and returns nothing.
  67.                                     AlignDirect_() makes it possible to declare a
  68.                                     variable with an initial value
  69.                                     ("int *x = (int*) AlignDirect_( &alignedX );").
  70.                                     Used by the new ALIGN macro. By the way,
  71.                                     AlignDirect_() calls Align_() to do its work.
  72.                                     
  73.                                     Added a new macro: ALIGN. It works much like the
  74.                                     Aligned template except it's a macro, so it works
  75.                                     under C as well as C++.
  76.     wolf        Sat, Jan 22, 2000    Rewrote AlignX() to be faster.
  77.     wolf        Sun, Jan 23, 2000    Added Pad typedef.
  78.     wolf        Sun, Jan 30, 2000    Carbonized.
  79.     wolf        Fri, Feb 18, 2000    Added IsAligned().
  80.     wolf        Mon, Dec 11, 2000    Align 1.0.
  81.     wolf        Thu, Feb 8, 2001    Align 1.0.2. CodeWarrior Pro 6 breaks an idiom:
  82.                                     implicit casting of <type>** to void**. I had to make
  83.                                     all such casts explicit.
  84.     
  85.     ************************************************************************************/
  86.  
  87. #ifndef        _Align_
  88. #define        _Align_
  89.  
  90. /**************************
  91. *    
  92. *    Types
  93. *    
  94. **************************/
  95. #pragma mark    (Types)
  96.  
  97. typedef    UInt8    Pad;
  98. typedef    UInt16    Pad2;
  99. typedef    UInt32    Pad4;
  100. typedef    UInt32    Pad8[ 2 ];
  101. typedef    UInt32    Pad16[ 4 ];
  102.  
  103. /**************************
  104. *    
  105. *    Interfaces
  106. *    
  107. **************************/
  108. #pragma mark    -
  109. #pragma mark    (Interfaces)
  110.  
  111. #ifdef        __cplusplus
  112. extern    "C"    {
  113. #endif    //    __cplusplus
  114.  
  115.     extern
  116.     void
  117. Align2(
  118.     void    **ptr );
  119.  
  120.     extern
  121.     void*
  122. AlignDirect2(
  123.     void    *ptr );
  124.  
  125.     extern
  126.     void
  127. Align4(
  128.     void    **ptr );
  129.  
  130.     extern
  131.     void*
  132. AlignDirect4(
  133.     void    *ptr );
  134.  
  135.     extern
  136.     void
  137. Align8(
  138.     void    **ptr );
  139.  
  140.     extern
  141.     void*
  142. AlignDirect8(
  143.     void    *ptr );
  144.  
  145.     extern
  146.     void
  147. Align16(
  148.     void    **ptr );
  149.  
  150.     extern
  151.     void*
  152. AlignDirect16(
  153.     void    *ptr );
  154.  
  155.     extern
  156.     void
  157. AlignX(
  158.     void    **ptr,
  159.     UInt32    alignment );
  160.  
  161.     extern
  162.     void*
  163. AlignXDirect(
  164.     void    *ptr,
  165.     UInt32    alignment );
  166.  
  167.     extern
  168.     OSErr
  169. NewAlignedPtr(
  170.     UInt32    size,
  171.     Boolean    fromSystemHeap,
  172.     Boolean    clearMemory,
  173.     UInt8    alignment,
  174.     void    **ptr );
  175.  
  176.     extern
  177.     Ptr
  178. GetUnalignedPtr(
  179.     void    *ptr );
  180.  
  181.     extern
  182.     Boolean
  183. IsAligned(
  184.     void    *ptr,
  185.     UInt8    alignment );
  186.  
  187. #ifdef    __cplusplus
  188. }
  189. #endif
  190.  
  191. /**************************
  192. *    
  193. *    DeclareAlignedVariable Macro
  194. *    
  195. **************************/
  196. #pragma mark    -
  197. #pragma mark    (DeclareAlignedVariable Macro)
  198.  
  199. #ifdef    __cplusplus
  200.     #include    <new>
  201.     #define    DeclareAlignedVariable( TYPE, NAME, ALIGNMENT )                        \
  202.                 struct    Aligned##TYPE##NAME {                                    \
  203.                     Pad        padding[ ALIGNMENT ];                                \
  204.                     char    value[sizeof(TYPE)];                                \
  205.                 };                                                                \
  206.                 struct Aligned##TYPE##NAME aligned##NAME;                        \
  207.                 TYPE *NAME = (TYPE*) AlignXDirect( &aligned##NAME, ALIGNMENT );    \
  208.                 NAME = new ( NAME ) TYPE
  209. #else
  210.     #define    DeclareAlignedVariable( TYPE, NAME, ALIGNMENT )    \
  211.                 struct    Aligned##TYPE##NAME {                \
  212.                     Pad        padding[ ALIGNMENT ];            \
  213.                     TYPE    value;                            \
  214.                 };                                            \
  215.                 struct Aligned##TYPE##NAME aligned##NAME;    \
  216.                 TYPE *NAME = (TYPE*) AlignXDirect( &aligned##NAME, ALIGNMENT )
  217. #endif
  218.  
  219. /**************************
  220. *    
  221. *    ALIGN macro
  222. *    
  223. **************************/
  224. #pragma mark    -
  225. #pragma mark    (ALIGN macro)
  226.  
  227. #ifdef    __cplusplus
  228.     #include    <new>
  229.     #define        ALIGN( TYPE, NAME, ALIGNMENT )        \
  230.         struct    Aligned##TYPE    {                    \
  231.             Pad##ALIGNMENT    padding;                \
  232.             char            value[sizeof(TYPE)];    \
  233.         };                                            \
  234.         struct    Aligned##TYPE    aligned##NAME;        \
  235.         TYPE                    *NAME = (TYPE*) AlignDirect##ALIGNMENT( &aligned##NAME );    \
  236.         NAME = new ( NAME ) TYPE
  237. #else
  238.     #define        ALIGN( TYPE, NAME, ALIGNMENT )    \
  239.         struct    Aligned##TYPE    {                \
  240.             Pad##ALIGNMENT    padding;            \
  241.             TYPE            value;                \
  242.         };                                        \
  243.         struct    Aligned##TYPE    aligned##NAME;    \
  244.         TYPE                    *NAME = (TYPE*) AlignDirect##ALIGNMENT( &aligned##NAME )
  245. #endif
  246.  
  247. #endif    //    _Align_